package com.dlb.andorid.activity;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dlb.andorid.App;
import com.dlb.andorid.ProductAdapter;
import com.dlb.andorid.R;
import com.dlb.andorid.XContract;
import com.dlb.andorid.bean.PeripheryShop;
import com.dlb.andorid.bean.ProductBean;
import com.dlb.andorid.bean.ServerResult;
import com.dlb.andorid.presenter.ZbhdPresenter;
import com.kwan.xframe.mvp.presenter.BasePresenter;
import com.kwan.xframe.mvp.view.activity.BaseCommonActivity;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

public class ZbhdInfoActivity extends BaseCommonActivity implements XContract.IZbhdView, OnRefreshListener, OnLoadMoreListener {

	ZbhdPresenter mPresenter;
	PeripheryShop mShop;

	@Override
	protected void beForeSetContentView() {
		super.beForeSetContentView();
		mShop = (PeripheryShop) getIntentData("data");
	}

	@Override
	protected int getTopViewId() {
		return 0;
	}

	@Override
	protected int getMainViewId() {
		return R.layout.activity_zbhd_info;
	}

	@Override
	protected int getBottomViewId() {
		return 0;
	}

	@Override
	protected BasePresenter getBasePresenter() {
		mPresenter = new ZbhdPresenter(this);
		return mPresenter;
	}


	protected RefreshLayout refreshLayout;
	protected RecyclerView mRecyclerView;
	protected BaseQuickAdapter mAdapter;
	protected boolean isLoadMore;
	protected boolean hasMoreData = true;
	public boolean isRefreshing;

	private List<ProductBean> mProducts = new ArrayList<>();

	@Override
	protected void initViews() {
		super.initViews();

		mImageLoader.loadUrl(mShop.getPeriphery_shop_pic(), (ImageView) findViewById(R.id.iv_icon));

		refreshLayout = findViewById(R.id.refreshLayout);
		mRecyclerView = findViewById(R.id.recycleView);
		mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2, LinearLayoutManager.VERTICAL, false));
		mRecyclerView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		mRecyclerView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
		mRecyclerView.setLayerType(View.LAYER_TYPE_NONE, null);

		mRecyclerView.setHasFixedSize(true);
		mRecyclerView.setItemAnimator(null);
		mRecyclerView.setNestedScrollingEnabled(false);

		mAdapter = new ProductAdapter(mProducts);

		mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
			@Override
			public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
				//toastMsg(""+mProducts.get(position).getId());
				if (view.getId() == R.id.iv_car) {
					showProgress("");
					mPresenter.addCar(mProducts.get(position).getId(), App.mUserInfo.getUid(), 1, mProducts.get(position).getType_id(), mProducts.get(position).getSale_price());
				}

			}
		});

		mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
			@Override
			public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
				Bundle bundle = new Bundle();
				bundle.putLong("id", mProducts.get(position).getId());
				go2Activity(ProductDetailActivity.class, bundle, false);
			}
		});


		mRecyclerView.setAdapter(mAdapter);

		refreshLayout.setOnRefreshListener(this);
		refreshLayout.setOnLoadMoreListener(this);
	}

	int page = 1;

	@Override
	protected void initData() {
		mPresenter.getZbhdInfo(mShop.getPeriphery_shop_id(), page);
	}

	@Override
	protected String getTitleTxt() {
		return "周边好店";
	}


	@Override
	public String getPageName() {
		return null;
	}

	@Override
	public void getZbhdListSuccess(List<PeripheryShop> data) {

	}

	@Override
	public void getZbhdInfoSuccess(List<ProductBean> data) {

		if (isLoadMore) {
			mProducts.addAll(data);
			mAdapter.addData(data);
		} else {
			mProducts.clear();
			mProducts.addAll(data);
			mAdapter.setNewData(data);
		}

		refreshLayout.finishRefresh();
		refreshLayout.finishLoadMore();

	}

	@Override
	public void addCarSuccess(ServerResult data) {
		dismissProgress();
		toastMsg(data.getMsg());
	}


	@Override
	public void onRefresh(@NonNull RefreshLayout refreshLayout) {
		isLoadMore = false;
		page = 1;
		mPresenter.getZbhdInfo(mShop.getPeriphery_shop_id(), page);
	}

	@Override
	public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
		isLoadMore = true;
		page++;
		mPresenter.getZbhdInfo(mShop.getPeriphery_shop_id(), page);
	}
}
